package com.flyme.common.core.aliyun.sms;

import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class AliSmsUtil {
    private static final String DOMAIN="dysmsapi.aliyuncs.com";
    private static final String VERSION="2017-05-25";
    private static final String SIGNNAME="勤博教育";
    public  static IAcsClient getClient(){
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIdBOeU021BuTh", "93QYRInZ8wYfPvi0cwbovjnvnWIS39");
        IAcsClient client = new DefaultAcsClient(profile);
        return  client;
    }
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIdBOeU021BuTh", "93QYRInZ8wYfPvi0cwbovjnvnWIS39");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", "18790286336");
        request.putQueryParameter("SignName",SIGNNAME);
        request.putQueryParameter("TemplateCode","SMS_199197961");
        String json="{\"mtname\":\"案例考试\",\"mttime\":\"2020-08-15\",\"submittime\":\"19:00-21:00\"}";
        request.putQueryParameter("TemplateParam", json);
//        request.putQueryParameter("mtname","案例考试");
//        request.putQueryParameter("mttime","2020-08-15");
//        request.putQueryParameter("submittime","19:00-21:00");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送验证码
     * @param smsTempEnum
     * @param objects
     */
    public static Boolean sendSmsCode(SmsTempEnum smsTempEnum,Object...objects) {
        Boolean tag=false;
        IAcsClient client = getClient();
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers",objects[0].toString());
        request.putQueryParameter("SignName", SIGNNAME);
        request.putQueryParameter("TemplateCode", smsTempEnum.getCode());
        if(objects.length>1){
            String params= JSONUtil.toJsonStr(objects[1]);
            request.putQueryParameter("TemplateParam", params);
        }
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            tag=true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  tag;
    }

}